package xconsole

import (
	"fmt"
	"os"
	"strings"

	"gitee.com/xiaoyutab/xgotool/xnum"
	"gitee.com/xiaoyutab/xgotool/xstring"
)

// 根据参数直接生成参数绑定、结构体等相关参数
func cpp(help bool) string {
	if help {
		return "根据参数直接生成参数绑定、结构体等相关参数"
	}
	params := os.Args[1:]
	if len(params) <= 0 {
		panic("请输入正确的参数列表，格式为：参数名1 [类型] 参数名2 [类型]...\nPS：类型参数默认为字符串，若输入int、float等类型会自动转为json.Number，再通过新的变量转回为指定类型\nPS2：目前不支持复合类型，若需要使用复合类型请使用json2model进行生成")
	}
	p := "\ttype temp_struct struct {"       // 接受参数变量
	pt := "\ttype temp_struct_used struct {" // 转换目标变量
	used := false
	json_num := []string{"int", "uint", "int8", "uint8", "int16", "uint16", "int32", "uint32", "int64", "uint64", "float32", "float64"}
	param_length := len(params)
	for i := 0; i < param_length; i++ {
		p_name := params[i]
		temp1 := xstring.CamelCase(p_name) + "\t"
		temp2 := xstring.CamelCase(p_name) + "\t"
		if param_length > i+1 {
			// 存在下一个变量
			if xnum.InArray(params[i+1], json_num) {
				temp1 += "json.Number"
				temp2 += params[i+1]
				used = true
				i++ // 跳过下一个变量
			} else if params[i+1] == "string" {
				temp1 += "string"
				temp2 += "string"
				i++
			} else {
				temp1 += "string"
				temp2 += "string"
			}
		} else {
			temp1 += "string"
			temp2 += "string"
		}
		temp1 += "\t`json:\"" + p_name + "\" form:\"" + p_name + "\" xml:\"" + p_name + "\"`\t//"
		temp2 += "\t`json:\"" + p_name + "\" form:\"" + p_name + "\" xml:\"" + p_name + "\"`\t//"
		p += "\n\t\t" + temp1
		pt += "\n\t\t" + temp2
	}
	p += "\n\t}"
	pt += "\n\t}"
	if used {
		p += "\n" + pt
	}
	// 追加变量实现
	p += "\n\ttemp := temp_struct{}"
	p += "\n\tif err := xgin.Bind(c, &temp); err != nil {"
	p += "\n\t\treturn xgin.Resp().Error(err)"
	p += "\n\t}"
	if used {
		p += "\n\t// 正式变量赋值"
		p += "\n\t// 请使用param进行参数验证、变量效验等操作"
		p += "\n\tparam := temp_struct_used{}"
		// pt字符串切割
		ptmp := strings.Split(pt, "\n")
		ptlen := len(ptmp)
		for i := 0; i < ptlen; i++ {
			if i == 0 || i+1 == ptlen {
				continue
			}
			temp := strings.Split(ptmp[i], "\t")
			if xnum.InArray(temp[3], json_num) {
				p += "\n\tparam." + temp[2] + " = xstring.J" + xstring.CamelCase(temp[3]) + "(temp." + temp[2] + ")"
			} else {
				p += "\n\tparam." + temp[2] + " = temp." + temp[2]
			}
		}
	} else {
		p += "\n\t// 正式变量赋值"
		p += "\n\t// 请使用param进行参数验证、变量效验等操作"
		p += "\n\tparam := temp"
	}

	fmt.Println(p)
	return ""
}
